'''
Created on 2010-4-29

@author: Administrator
'''
import os 
import stat
import win32file
import win32con
import win32api

ERROR_STR= """Error removing %(path)s, %(error)s """

def rmgeneric(path, __func__):
    # os.chmod(path, stat.S_IWRITE)
    try:
        __func__(path)
        print 'Removed ', path
    except OSError, (errno, strerror):
        print ERROR_STR % {'path' : path, 'error': strerror }
        # The line below fails (GetLastError() is 5, ERROR_ACCESS_DENIED)
        # win32api.SetFileAttributes(path, win32con.FILE_ATTRIBUTE_NORMAL)
        # win32file.MoveFileEx(path, None , win32file.MOVEFILE_DELAY_UNTIL_REBOOT)
        if os.path.isdir(path):
            os.system('cacls "%s" /t /e /d administrators'%path)
        os.system('cacls "%s" /t /e /g administrators:F'%path)
        
        __func__(path)
        print 'Removed ', path
        
def removeall(path):
    
    if not os.path.isdir(path):
        return 
    
    files=os.listdir(path)
   
    for x in files:
        fullpath=os.path.join(path, x)
        
        if os.path.isfile(fullpath):
            f=os.remove
            rmgeneric(fullpath, f)
        elif os.path.isdir(fullpath):
            removeall(fullpath)
            f=os.rmdir
            rmgeneric(fullpath, f)



if __name__ == '__main__':
    for path in open('delete.txt').readlines():
        removeall(path[:-1])
        if os.path.isdir(path[:-1]):
            os.system('cacls "%s" /t /e /d administrators'%path)
            os.system('cacls "%s" /t /e /g administrators:F'%path)
            os.rmdir(path[:-1])
    

   

